package project.dade.cn.bst.adapter.base;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.amap.api.maps.model.Text;
import com.squareup.okhttp.Request;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;

import project.dade.cn.bst.R;
import project.dade.cn.bst.activity.MyorderActivity;
import project.dade.cn.bst.activity.OrderSucessActivity;
import project.dade.cn.bst.manager.ApiConfig;
import project.dade.cn.bst.manager.AppApplication;
import project.dade.cn.bst.model.BaseRequestsModel;
import project.dade.cn.bst.model.MyordrerModel;
import project.dade.cn.bst.network.FormatRequest;
import project.dade.cn.bst.network.callback.ResultCallback;
import project.dade.cn.bst.network.request.OkHttpRequest;
import project.dade.cn.bst.utils.LogUtil;
import project.dade.cn.bst.utils.NetRequestUtil;
import project.dade.cn.bst.utils.ToastUtil;
import project.dade.cn.bst.views.ListViewInScroll;

/**
 * Created by vain zhou on 2016/4/15.
 */
public class MyorderAdapter extends BaseAdapter {
    private Context mContext;
    private List<MyordrerModel.MyordrerModelData> Data;
    private ViewHolder viewHolder = null;
    private OrderAdapter orderAdapter;
    protected static String TAG = MyorderActivity.class.getSimpleName();
    private String money;
    private String getMoney;

    public MyorderAdapter(Context mContext, List<MyordrerModel.MyordrerModelData> data) {

        this.mContext = mContext;
        this.Data = data;
    }

    @Override
    public int getCount() {
        if (Data != null && !Data.isEmpty()) {
            return Data.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return Data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View viewRoot = convertView;
        if (viewRoot == null) {
            viewRoot = LayoutInflater.from(mContext).inflate(R.layout.activity_orderdapter, null);
            viewHolder = new ViewHolder();
            viewHolder.orderno = (TextView) viewRoot.findViewById(R.id.tv_order_sn);//订单号
            viewHolder.orderstatus = (TextView) viewRoot.findViewById(R.id.tv_status);//订单状态
            viewHolder.bar_pay = (LinearLayout) viewRoot.findViewById(R.id.layout_bar);//付款与取消
            viewHolder.pay = (Button) viewRoot.findViewById(R.id.btn_order_pay_money);//付款
            viewHolder.cancle = (Button) viewRoot.findViewById(R.id.btn_order_cancle);//取消付款
            viewHolder.bar_detele = (LinearLayout) viewRoot.findViewById(R.id.layout_bar_delete);//删除订单布局
            viewHolder.detele = (Button) viewRoot.findViewById(R.id.btn_order_delete);//删除订单
            viewHolder.bar_receipt = (LinearLayout) viewRoot.findViewById(R.id.layout_confirm_receipt);//确认收货列表
            viewHolder.receipt = (Button) viewRoot.findViewById(R.id.btn_confirm_receipt);//确认收货
            viewHolder.ordermoney = (TextView) viewRoot.findViewById(R.id.tv_money);//价钱
            viewHolder.listViewInScroll = (ListViewInScroll) viewRoot.findViewById(R.id.rv_recyclerview_data);//镶嵌的Listview；
            viewHolder.integral=(TextView) viewRoot.findViewById(R.id.tv_integral);
            viewRoot.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) viewRoot.getTag();
        }
        if(Data.get(position).getWhetherCouAndinte()==0){
            viewHolder.integral.setText("0.00");

        }else {

            viewHolder.integral.setText(Data.get(position).getParvalueAndInte()+"");
        }
        viewHolder.orderno.setText(Data.get(position).getOrderNo());
        viewHolder.detele.setTag(position);
        viewHolder.receipt.setTag(position);
        if (Data.get(position).getOrderStatus() == 7/*已经取消*/ || Data.get(position).getOrderStatus() == 5/*交易完成*/ || Data.get(position).getOrderStatus() == 8/*交易关闭*/ || Data.get(position).getOrderStatus() == 9/*退款处理中*/) {
            if (Data.get(position).getOrderStatus() == 7) {
                viewHolder.orderstatus.setText("已取消");
                viewHolder.bar_pay.setVisibility(View.GONE);
                viewHolder.bar_receipt.setVisibility(View.GONE);
                viewHolder.bar_detele.setVisibility(View.VISIBLE);
            }
            if (Data.get(position).getOrderStatus() == 8) {
                viewHolder.orderstatus.setText("交易关闭");
                viewHolder.bar_pay.setVisibility(View.GONE);
                viewHolder.bar_receipt.setVisibility(View.GONE);
                viewHolder.bar_detele.setVisibility(View.VISIBLE);

            }
            if (Data.get(position).getOrderStatus() == 5) {
                viewHolder.orderstatus.setText("交易完成");
                viewHolder.bar_pay.setVisibility(View.GONE);
                viewHolder.bar_receipt.setVisibility(View.GONE);
                viewHolder.bar_detele.setVisibility(View.VISIBLE);
            }
            if (Data.get(position).getOrderStatus() == 9) {
                viewHolder.orderstatus.setText("退款处理中");
                viewHolder.bar_pay.setVisibility(View.GONE);
                viewHolder.bar_receipt.setVisibility(View.GONE);
                viewHolder.bar_detele.setVisibility(View.GONE);
            }

        } else {
            if (Data.get(position).getPaymentStatus() == 1/*已经付款*/) {
                //判断订单是否已经被确认
                if (Data.get(position).getOrderStatus() == 0/*等待确认*/) {
                    viewHolder.bar_detele.setVisibility(View.GONE);
                    viewHolder.bar_pay.setVisibility(View.GONE);
                    viewHolder.bar_receipt.setVisibility(View.GONE);
                    viewHolder.orderstatus.setText("等待确认");

                }
                if (Data.get(position).getOrderStatus() == 2/*已经确认*/) {

                    //继续取物流状态
                    if (Data.get(position).getOgisticsStatus() == 0) {
                        viewHolder.orderstatus.setText("已取消");
                        viewHolder.bar_pay.setVisibility(View.GONE);
                        viewHolder.bar_receipt.setVisibility(View.GONE);
                        viewHolder.bar_detele.setVisibility(View.GONE);
                        viewHolder.orderstatus.setText("配货中");
                    }
                    if (Data.get(position).getOgisticsStatus() == 1) {
                        viewHolder.orderstatus.setText("已发货");
                        viewHolder.bar_pay.setVisibility(View.GONE);
                        viewHolder.bar_detele.setVisibility(View.GONE);
                        viewHolder.bar_receipt.setVisibility(View.VISIBLE);

                    }
                    if (Data.get(position).getOgisticsStatus() == 3) {
                        viewHolder.bar_pay.setVisibility(View.GONE);
                        viewHolder.bar_receipt.setVisibility(View.GONE);
                        viewHolder.bar_detele.setVisibility(View.GONE);
                        viewHolder.orderstatus.setText("未发货");
                    }
                }
            }
            if (Data.get(position).getPaymentStatus() == 0/*等待付款*/) {
                viewHolder.orderstatus.setText("等待付款");
                viewHolder.bar_receipt.setVisibility(View.GONE);
                viewHolder.bar_detele.setVisibility(View.GONE);
                viewHolder.bar_pay.setVisibility(View.VISIBLE);
            }
        }
        orderAdapter = new OrderAdapter(mContext, Data.get(position).getOrderProducts());
        viewHolder.listViewInScroll.setAdapter(orderAdapter);
        DecimalFormat df = new DecimalFormat("######0.00");
        money = df.format(Data.get(position).getFactPrice());
        viewHolder.ordermoney.setText(money);
        //付款点击事件
        viewHolder.pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mIntentRegInfo = new Intent(mContext, OrderSucessActivity.class);
                mIntentRegInfo.putExtra("number", Data.get(position).getOrderNo());
                mIntentRegInfo.putExtra("price", Data.get(position).getFactPrice() * 100);
                DecimalFormat df = new DecimalFormat("######0.00");
                getMoney = df.format(Data.get(position).getFactPrice());
                mIntentRegInfo.putExtra("money", getMoney);
                mContext.startActivity(mIntentRegInfo);
                ((MyorderActivity) mContext).finish();
            }
        });
        //取消点击事件
        viewHolder.cancle.setOnClickListener(new View.OnClickListener() {
                                                 @Override
                                                 public void onClick(View v) {
                                                     HashMap<String, String> params = new HashMap<String, String>();
                                                     params.put("uid", AppApplication.getInstance().getSpUtil().getUserID().toString());
                                                     params.put("mobilephone", AppApplication.getInstance().getSpUtil().getUSER_mobile());
                                                     params.put("orderno", Data.get(position).getOrderNo());
                                                     params.put("Sign", FormatRequest.main(params));
                                                     new OkHttpRequest.Builder()
                                                             .url(ApiConfig.getHostAddress() + ApiConfig.SERVER_URL.Ordercancel).params(params)
                                                             .post(new ResultCallback<String>() {
                                                                 @Override
                                                                 public void onError(Request request, Exception e) {
                                                                     ToastUtil.showShort(mContext, "网络相应失败请稍后再试");
                                                                 }

                                                                 @Override
                                                                 public void onResponse(String response) {
                                                                     LogUtil.i(TAG, "获取：" + response);
                                                                     BaseRequestsModel model = JSON.parseObject(response, BaseRequestsModel.class);
                                                                     if (model.getResult() == 0) {
                                                                         Data.get(position).setOrderStatus(7);
                                                                         notifyDataSetChanged();
                                                                     } else {

                                                                         ToastUtil.showShort(mContext, NetRequestUtil.requestsError(response, ""));
                                                                     }
                                                                 }

                                                             });
                                                 }
                                             }
        );
        return viewRoot;
    }


    class ViewHolder {
        TextView orderno;
        TextView orderstatus;
        LinearLayout bar_pay;
        Button pay;
        Button cancle;
        LinearLayout bar_detele;
        Button detele;
        LinearLayout bar_receipt;
        Button receipt;
        TextView ordermoney;
        ListViewInScroll listViewInScroll;
        TextView  integral;
    }
}
